package com.tjetc.controller;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.tjetc.common.JsonResult;
import com.tjetc.entity.Order;
import com.tjetc.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.ArrayList;

@RestController
@RequestMapping("admin/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    /**
     * 分页查询接口
     * @param order 传入的条件对象
     * @param pageNum 第几页
     * @param pageSize 每页中元素的数量
     * @return 返回分页的封装信息
     */
    @RequestMapping("page")
    public JsonResult page(Order order,
                            @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                            @RequestParam(value = "pageSize", required = false, defaultValue = "5") Integer pageSize){
        return orderService.page(order, pageNum, pageSize);
    }

    /**
     * 更新订单
     * @param order 输入前端处理过的订单form
     * @return 返回更新结果
     */
    @RequestMapping("update")
    public JsonResult update(Order order) {
        return new JsonResult(0, "", orderService.updateOrder(order));
    }

    /**
     * 添加订单
     * @param order 指定添加的订单
     * @return 返回添加订单的结果消息
     */
    @RequestMapping("insert")
    public JsonResult insert(Order order) {
        return new JsonResult<>(0, "", orderService.add(order));
    }

    /**
     * 根据checkoutTime查询订单
     * @param id 指定商家id
     * @param time 指定时间（按日期）
     * @return 返回包含查询结果List的Json结果对象
     */
    @RequestMapping("searchOrderByCheckoutTime")
    public JsonResult searchOrderByCheckoutTime(@RequestParam("merchantId") Integer id,
                                                @RequestParam(value = "time", required = false)
                                                //@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
                                                @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime time) {
        return new JsonResult(0, "", orderService.searchOrderByCheckoutTime(id, time));
    }

    /**
     * 根据ordeId查询订单
     * @param id 输入的订单id
     * @return 返回订单id的封装Json对象
     */
    @RequestMapping("selectById")
    public JsonResult selectById(@RequestParam("id") Integer id) {
        return new JsonResult(0, "", orderService.selectByOrderId(id));
    }
}
